package com.bc.web.controller.admin;

import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSJson;
import com.fs.common.util.FSJedis;
import com.bc.web.config.R;
import com.bc.web.model.Channel;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

public class ChannelController extends FSController{
	
	public void search(){
		Page<Channel> items = Channel.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", items);
		render("basedata/channelList.html");
	}
	
	public void addPage(){
		render("basedata/channelAdd.html");
	}
	
	public void updatePage(){
		Channel channel = Channel.dao.findById(getPara("id"));
		setAttr("channel", channel);
		render("basedata/channelUpdate.html");
	}
	
	@Before(Tx.class)
	public void add(){
		Channel channel = getModel(Channel.class);
		if(Channel.dao.selectByCode(channel.getCode()) != null){
			renderFail("渠道号已存在");
		}else {
			channel.setUpdateTime(FSDate.getDateTime());
			channel.save();
			if (R.IS_REDIS) {
				FSJedis.update(R.RK_CHANNEL, channel.getCode(), FSJson.toString(channel));
			}
			renderSuccess();
		}
	}
	
	@Before(Tx.class)
	public void update(){
		Channel channel = getModel(Channel.class);
		channel.setUpdateTime(FSDate.getDateTime());
		channel.update();
		if(R.IS_REDIS) {
			FSJedis.update(R.RK_CHANNEL, channel.getCode(), FSJson.toString(channel));
		}
		renderSuccess();
	}
	
	@Before(Tx.class)
	public void delete(){
		Channel channel = Channel.dao.findById(getPara("id"));
		Channel.dao.deleteById(channel.getId());
		if(R.IS_REDIS) {
			FSJedis.remove(R.RK_CHANNEL, channel.getCode());
		}
		renderSuccess(false);
	}
	
}
